package com.twb.auth.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.twb.auth.entity.twbPlatform.TwbPlatform;
import com.twb.auth.entity.twbPlatform.dto.ListTwbPlatformDto;
import com.twb.auth.entity.twbPlatform.dto.SearchTwbPlatformDto;
import com.twb.auth.entity.twbPlatform.vo.ListTwbPlatformVo;
import com.twb.auth.entity.twbPlatform.vo.SaveTwbPlatformVo;
import com.twb.auth.entity.twbPlatform.vo.SearchTwbPlatformVo;
import com.twb.auth.entity.twbPlatform.vo.UpdateTwbPlatformByIdVo;
import com.twb.auth.mapper.TwbPlatformMapper;
import com.twb.auth.service.ITwbPlatformService;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.TwbPageUtil;
import com.twb.fileCenter.entity.vo.ExportExcelVo;

/**
 * @ClassName TwbPlatformService
 * @Description 平台信息service
 * 
 * @author DSY
 * @date 2020-11-04 17:55:18
 */
@Service("twbPlatformService")
public class TwbPlatformService extends BaseService<TwbPlatformMapper,TwbPlatform> implements ITwbPlatformService {

	@Autowired
	TwbPlatformMapper twbPlatformMapper;

	@Override
	public TwbPageInfo<SearchTwbPlatformDto> searchTwbPlatform(SearchTwbPlatformVo vo) {
		TwbPageUtil.startPage(vo);
		List<SearchTwbPlatformDto> list = twbPlatformMapper.searchTwbPlatform(vo);
		return TwbPageUtil.of(list);
	}

	@Override
	public List<SearchTwbPlatformDto> searchTwbPlatformList(SearchTwbPlatformVo vo) {
		List<SearchTwbPlatformDto> list = twbPlatformMapper.searchTwbPlatform(vo);
		return list;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveTwbPlatform(SaveTwbPlatformVo vo) {

		TwbPlatform checkVo = new TwbPlatform();
		checkVo.setCode(vo.getCode());
		checkVo.setIsdel(0);
		long count = this.count(checkVo);
		if (count > 0) {
			throw new ServiceException(String.format("[%s]已存在", checkVo.getCode()));
		}

		TwbPlatform record = new TwbPlatform();
		BeanUtils.copyNotNullProperties(vo, record);
		record.setId(this.getUUID());
		this.setCreateInfo(record);
		this.setUpdateInfo(record);
		List<TwbPlatform> entityList = new ArrayList<>();
		entityList.add(record);
		return this.saveBatch(entityList );
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateTwbPlatformById(UpdateTwbPlatformByIdVo vo) {
		TwbPlatform record = twbPlatformMapper.selectById(vo.getId());
		if (null == record) {
			throw new ServiceException("数据不存在");
		}

		if (!vo.getName().equalsIgnoreCase(record.getName())) {
			TwbPlatform checkVo = new TwbPlatform();
			checkVo.setName(vo.getName()); // 此处需要修改为需要校验重复的字段
			checkVo.setIsdel(0);
			long count = this.count(checkVo);
			if (count > 0) {
				throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
			}
		}

		BeanUtils.copyProperties(vo, record);
		this.setUpdateInfo(record);
		return this.updateById(record);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteTwbPlatformById(String id) {
		return this.removeById(id);
	}

	@Override
	public boolean deleteTwbPlatformByIds(String ids) {

		return this.removeByIds(Arrays.asList(ids.split(",")));
	}

	@Override
	public int logicBatchDeleteTwbPlatformByIds(String ids) {
		List<String> list = new ArrayList<String>();
		Collections.addAll(list, ids.split(","));
		return twbPlatformMapper.logicBatchDeleteByIds(list);
	}

	@Override
	public void exportExcel(HttpServletRequest request, ExportExcelVo exportExcelVo, SearchTwbPlatformVo searchVo) {

	}

	@Override
	public int importExcel(Map<String, String> map) {
		System.out.println(map.toString());
		return 0;
	}

	@Override
	public List<ListTwbPlatformDto> listTwbPlatform(ListTwbPlatformVo vo) {

		return twbPlatformMapper.listTwbPlatform(vo);
	}
}
